var router =require('express').Router();
var async=require('async');
var Book=require('../model/book');
var Comment=require('../model/comment');
var cats=require('../cats');

module.exports = router;
router.get('/',(req, res,next)=>{
    async.parallel({
        news:(newCb)=>{
            async.map(cats,(cat,catCb)=>{
                console.log('cat:',cat);
                Book.page({'cat.cn':cat.cn},{market:true,putaway:true},{sort:{putaway:-1},limit:16},(err,books)=>{
                    if(err)return catCb(err);
                    catCb(err,books);
                });
            },(err, booksList)=>{
                if(err)return newCb(err);
                newCb(null,booksList.map((books)=>{
                        return {
                            top8:books.slice(0,8),
                            last:books.slice(8)
                        }
                    })
                );
            });
        },
        recommended:(recommendedCb)=>{
            async.map(cats,(cat,catCb)=>{
                async.waterfall([
                    (wCb)=>{
                        Book.page({'cat.cn':cat.cn},{sku:true,name:true,sales:true,comment:true},{sort:{sales:-1},limit:20},(err,books)=>{
                            if(err)return wCb(err);
                            var commentIds=books.map((book)=>{
                                return book.comment;
                            });
                            wCb(null,commentIds);
                        });
                    },
                    (commentIds,wCb)=>{
                        Comment.page({_id:{$in:commentIds}},{book:true,goodRate:true},{sort:{goodRate:-1}},(err,comments)=>{
                            if(err)return wCb(err);
                            var ids=comments.map((comment)=>{
                                return comment.book;
                            });
                            wCb(null,ids);
                        });
                    },
                    (ids,wCb)=>{
                        Book.page({_id:{$in:ids}},{_id:true},{},(err,books)=>{
                            if(err)return wCb(err);
                            ids=ids.map((id)=>{return id.toString()});
                            books.sort((a,b)=>{//同步根据好评率排序查询到的ID顺序
                                return ids.indexOf(a._id.toString())<ids.indexOf(b._id.toString())?-1:1;
                            });
                            wCb(null,books);
                        });
                    }
                ],(err,books)=>{
                    if(err)return catCb(err);
                    catCb(err,books);
                });
            },(err, booksList)=>{
                if(err)return next(err);
                recommendedCb(null,booksList.map((books)=>{
                        return {
                            top8:books.slice(0,8),
                            last:books.slice(8)
                        }
                    })
                );
            });
        }
    },(err,result)=>{
        if(err)return next(err);
        var user=req.session.user || {name:req.signedCookies.username};
        console.log('result:',result);

        res.render('index',{title:'Index',user:user,result:result});
    });
});